﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using LeadVinaglue.Models;
using LeadVinaglue.Models.ViewPages;
using LeadVinaglue.Repository;
using LeadVinaglue.Utility;

namespace LeadVinaglue.Controllers
{
    public class RegisterController : BaseController
    {
        EFRoleProvider provider = new EFRoleProvider();
        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        // GET: /Register/
        [DeviceFilterAttribute(Order = 1)]
        [CompressFilter(Order = 2)]
        public ActionResult Index()
        {
            RegisterManagementRepository repository = new RegisterManagementRepository();
            RegisterManagementModel model = new RegisterManagementModel();

            // Setup common
            SetupCommon(model);

            repository.GetModel(ref model, LeadConstant.REGISTER);

            ViewBag.ErrorMessage = "";

            return View(model);
        }

        public ActionResult Success()
        {
            RegisterManagementRepository repository = new RegisterManagementRepository();
            RegisterManagementModel model = new RegisterManagementModel();

            // Setup common
            SetupCommon(model);

            repository.GetModel(ref model, LeadConstant.REGISTER);
            ViewBag.ErrorMessage = "Bạn vừa đăng ký tài khoản thành công. Xin vui lòng kiểm tra e-mail và kích hoạt tài khoản.";

            return View(model);
        }

        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            // Get Form
            RegisterModel log = new RegisterModel();

            log.Email = form["Email"].ToString();
            log.Fullname = form["Fullname"].ToString();
            log.Password = EncryptionData.Encryption(form["Password"].ToString());
            
            // Attempt to register the user
            MembershipCreateStatus createStatus = MembershipService.CreateUser(log.Email, log.Password, log.Email);

            if (createStatus == MembershipCreateStatus.Success)
            {
                // Assign role
                provider.AddUserToRole(log.Email, "ClientAdmin");

                // Send an email to User
                bool success = false;
                UserRepository umRepository = new UserRepository();
                var userModel = umRepository.GetSingle(log.Email);

                // Update Users
                userModel.Fullname = log.Fullname;
                userModel.GUID = Guid.NewGuid().ToString();
                umRepository.Save(userModel);

                UserEmailToken.SendEmail(userModel, "Register", ref success);

                // Redirect
                return RedirectToAction("Success", "Register");
            }
            else
            {
                ViewBag.ErrorMessage = "Tên đăng nhập đã được đăng ký. Xin vui lòng chọn tên đăng nhập khác.";
            }

            // If we got this far, something failed, redisplay form
            RegisterManagementRepository repository = new RegisterManagementRepository();
            RegisterManagementModel model = new RegisterManagementModel();

            // Setup common
            SetupCommon(model);

            repository.GetModel(ref model, LeadConstant.REGISTER);

            return View(model);
        }
    }
}
